package com.lmqb.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmqb.bean.LmUser;
import com.lmqb.bean.SysBlack;
import com.lmqb.bean.SysUser;
import com.lmqb.bean.SysWhiteList;
import com.lmqb.constant.MsgError;
import com.lmqb.entry.MenuTitle;
import com.lmqb.page.AgentAccountPager;
import com.lmqb.page.SysWhiteListrPager;
import com.lmqb.responces.ResultData;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.SysWhiteListService;
import com.lmqb.util.StrUtil;

//白名单控制层
@Controller
public class SysWhiteListController extends BaseController{
	
	@Autowired
	private SysWhiteListService sysWhiteListService;
	
	@Autowired
	private JiaoYanService jiaoYanService;
	
	//白名单列表
	@RequestMapping(value="sysWhiteList.html",method=RequestMethod.GET)
	public String sysWhiteList(HttpSession session,HttpServletRequest request,SysWhiteListrPager pagerModel){
		
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(SysWhiteListrPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			SysWhiteListrPager selectAll = sysWhiteListService.selectAll(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, selectAll);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("审核日志");
			menuTitle.setMenu("白名单列表");
			request.setAttribute("adminId",sysUser.getRoleId());
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "sysWhiteList";
	}
	
	
	//跳转添加页面
	@RequestMapping(value="addWhiteList",method=RequestMethod.GET)
	public String addWhiteList(HttpServletRequest request){
		System.out.println("添加白名单");
		String adminId = request.getParameter("adminId");
		request.setAttribute("adminId", adminId);
		return "addWhiteList";
	}
	
	//添加数据
	@RequestMapping(value="insertWhite",method=RequestMethod.POST)
	@ResponseBody
	public ResultData<String> insertWhite(HttpServletRequest request,String loanMobile,String userName,String adminId){
		ResultData<String> resultData=new ResultData<String>();
		
		try{
			SysWhiteList sysWhiteList=new SysWhiteList();
			sysWhiteList.setAdminId(Integer.parseInt(adminId));
			sysWhiteList.setLoanMobile(loanMobile);
			sysWhiteList.setUserName(userName);
			System.out.println("%%%%%%%%%%"+sysWhiteList.getLoanMobile());
			resultData=jiaoYanService.addWhiteList(sysWhiteList);
			if(resultData.getStatus()==1){
				return resultData;
			}
			LmUser selectLmUserByMobile = sysWhiteListService.selectLmUserByMobile(sysWhiteList.getLoanMobile());
			if(selectLmUserByMobile != null){
				sysWhiteList.setUserId(selectLmUserByMobile.getUserId());
				}
			sysWhiteList.setUserId(0);
			Integer sysBlack = sysWhiteListService.selectByMobile(sysWhiteList.getLoanMobile());
			
			if(sysBlack > 0){
				int i = sysWhiteListService.deleteByMobile(sysWhiteList.getLoanMobile());
				int insert = sysWhiteListService.insert(sysWhiteList);
			}else{
				int insert = sysWhiteListService.insert(sysWhiteList);
			}
		}catch(Exception e){
			e.printStackTrace();
			resultData.setError(MsgError.SysErr);
		}
		return resultData;
		
		//boolean mobile = StrUtil.isMobile(sysWhiteList.getLoanMobile());
		//System.out.println("正则判断*****"+mobile);
		/*LmUser selectLmUserByMobile = sysWhiteListService.selectLmUserByMobile(sysWhiteList.getLoanMobile());
		if(selectLmUserByMobile != null){
			sysWhiteList.setUserId(selectLmUserByMobile.getUserId());
			}
		sysWhiteList.setUserId(0);
		Integer sysBlack = sysWhiteListService.selectByMobile(sysWhiteList.getLoanMobile());
		
		if(sysBlack > 0){
			int i = sysWhiteListService.deleteByMobile(sysWhiteList.getLoanMobile());
			int insert = sysWhiteListService.insert(sysWhiteList);
		}else{
			int insert = sysWhiteListService.insert(sysWhiteList);
		}
		
		return "redirect:sysWhiteList.html";*/
	}
	
	//跳转修改页面
	@RequestMapping(value="updateWhiteList",method=RequestMethod.GET)
	public String updateWhiteList(HttpServletRequest request){
		String whiteId = request.getParameter("whiteId");
		String adminId = request.getParameter("adminId");
		System.out.println(whiteId+"-->"+adminId);
		SysWhiteList selectByPrimaryKey = sysWhiteListService.selectByPrimaryKey(Integer.parseInt(whiteId));
		System.out.println("修改白名单");
		request.setAttribute("MOBILE",selectByPrimaryKey);
		request.setAttribute("adminId",adminId);
		return "updateWhiteList";
	}
	
	//修改
	@RequestMapping(value="modifyWhiteList",method=RequestMethod.GET)
	public String modifyWhiteList(String adminId,String whiteId,String loanMobile){
		SysWhiteList sysWhiteList=new SysWhiteList();
		sysWhiteList.setAdminId(Integer.parseInt(adminId));
		sysWhiteList.setWhiteId(Integer.parseInt(whiteId));
		sysWhiteList.setLoanMobile(loanMobile);
		System.out.println(sysWhiteList);
		int i = sysWhiteListService.updateBySysWhiteList(sysWhiteList);
		return "redirect:sysWhiteList.html";
	}
	
	//删除
	@RequestMapping(value="deleteWhiteList",method=RequestMethod.GET)
	public String deleteWhiteList(HttpServletRequest request){
		String whiteId = request.getParameter("whiteId");
		String adminId = request.getParameter("adminId");
		System.out.println("删除"+whiteId+"-->"+adminId);
		SysWhiteList sysWhiteList=new SysWhiteList();
		sysWhiteList.setWhiteId(Integer.parseInt(whiteId));
		sysWhiteList.setAdminId(Integer.parseInt(adminId));
		int i = sysWhiteListService.deleteByPrimaryKey(sysWhiteList);
			return "redirect:sysWhiteList.html";
		}
	
}
